package online.sh.cn.fy.users.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;
import online.sh.cn.fy.common.utils.CookieUtil;
import online.sh.cn.fy.common.utils.DES3Util;
import online.sh.cn.fy.common.utils.ResponseUtils;
import online.sh.cn.fy.common.utils.TokenUtils;

/**
 * 记录用户积分详细数据
 */
@WebServlet("/userevents")
public class FyUserEventServlet extends HttpServlet {
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		String openid=CookieUtil.getCookieValueByName(request, "openid");
		JSONObject json=new JSONObject();
		if(null!=openid && ""!=openid){
			String uuid=CookieUtil.getCookieValueByName(request, "uuid");
			if(null!=uuid && uuid.equals("0")){
				try {
					openid=DES3Util.decrypt(openid);
				} catch (Exception e) {
					log(e.getMessage());
				}
				TokenUtils tu=new TokenUtils();
				Map user=tu.getFyUserinfoMap(openid);
				List events=tu.checkPointsEvent(openid);
				int changecounts=tu.CountProductByOpenid(openid);
				String result="";
				List pointsgood=new ArrayList();
				if(null!=events && events.size()!=0){
					for (int i = 0; i < events.size(); i++) {
						Map map=(Map) events.get(i);
						result+="<li class='clearfix minus'>"
								+"<div class='fl user-point-time'>"+map.get("logtime")+"</div>"
								+"<div class='fl user-point-title'>"+map.get("events")+"</div>"
								+"<div class='fr user-point-num'>"+map.get("points")+"</div></li>";
						if(map.get("product").equals("电信宽带氮气加速瓶")){
							pointsgood.add(map.get("points"));
						}
					}
				}else{
					result="";
				}
	            json.put("result", 0);
	            json.put("changecounts", changecounts);
	            json.put("events", result);
	            json.put("pointsgood", pointsgood);
	        	json.put("points", user.get("points").toString());
	        	json.put("headimg", user.get("headimg").toString());
	            ResponseUtils.renderJson(response, json.toString());
			}else{
				json.put("result", 1);
				ResponseUtils.renderJson(response, json.toString());
			}
		}else{
			json.put("result", 1);
			ResponseUtils.renderJson(response, json.toString());
		}
	}

	public static void main(String[] args) {
		List pointsgood=new ArrayList();
		System.out.println(pointsgood);
	}
}
